/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.csm.sim2k11.entities;

import com.csm.sim2k11.simulator.Configuration;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author santi
 */
public class Restaurant {

    private double nextClient;
    private Table[] tables;
    private ArrayList<Client> clientLine;

    public Restaurant() {
        nextClient = 0;
        clientLine = new ArrayList<Client>();
        tables = new Table[6];
        for (int i = 0; i < 6; i++) {
            tables[i] = new Table(i + 1);
        }
    }

    /**
     * Return how many minutes the next client will come.
     * @return Minutes
     */
    public double getNextClientTime() {
        return Configuration.getIntervalComing();
    }

    /**
     * Return the client's quantity that arrives the restaurant.
     * @param rnd Random number between 1 and 0
     * @return Client's quantity by random
     */
    public int getClientsQuantity(double rnd) {
        double probAcum = Configuration.getPCP()[0];//FDA de la llegada de clientes
        int clients = 0;
        for (int i = 1; i <= 4; i++) {
            if (rnd < probAcum) {
                clients = i;
                break;
            } else if (i < 4) {
                probAcum += Configuration.getPCP()[i];
            } else {
                clients = 4;
                break;
            }
        }
        return clients;
    }
    
    /**
     * Check the states of the tables, if there is any free table, it
     * returns that one. If there are no free tables, return null.
     * @return Table object with state FREE.
     */
    public Table getFreeTable()
    {
        Table freeTable = null;
        for (int i = 0; i < 6; i++) {
            if (tables[i].getState() == Table.FREE)
                freeTable = tables[i];
        }
        return freeTable;
    }

    /**
     * @return the clientLine
     */
    public ArrayList<Client> getClientLine() {
        return clientLine;
    }
    
    /**
     * Add a client to the waiting line
     */
    public void addClientToLine(Client client)
    {
        clientLine.add(client);
    }
    
    public Table[] getTables()
    {
        return tables;
    }
}
